package com.inspur.attendance.Aspects.LoginLog;




import com.inspur.attendance.Tools.IpAdrress;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;

@Aspect
@Component
public class LoginLog {

    @Autowired
    LogDao logDao;

    @Pointcut("@annotation(com.inspur.attendance.Tools.Operation)")
    public void loginPointCut() {}


    @After("loginPointCut()")
    public void loginLog(JoinPoint jp) {
        LogEntity logEntity = new LogEntity();

        //从切面织入点处通过反射机制获取织入点处的方法
        MethodSignature signature = (MethodSignature) jp.getSignature();
        //获取切入点所在的方法
        Method method = signature.getMethod();


        //获取请求的类名
        String className = jp.getTarget().getClass().getName();
        System.out.println("获取请求的类名: " + className);


        //获取请求的方法名
        String methodName = method.getName();
        switch (methodName) {
            case "LoginAuth":
                Object[] LoginAuth = jp.getArgs();
                logEntity.setOperator((String) LoginAuth[0]);
                logEntity.setAction("登录");
                break;
            case "delUser":
                Object[] delUser = jp.getArgs();
                logEntity.setOperator((String) delUser[0]);
                logEntity.setAction("删除用户");
                break;
            case "editUser":
                Object[] editUser = jp.getArgs();
                logEntity.setOperator((String) editUser[0]);
                logEntity.setAction("修改用户");
                break;
            case "addUser":
                Object[] addUser = jp.getArgs();
                logEntity.setOperator((String) addUser[0]);
                logEntity.setAction("添加用户");
                break;
            case "addDepartment":
                Object[] addDepartment = jp.getArgs();
                logEntity.setOperator((String) addDepartment[0]);
                logEntity.setAction("添加部门"+(String) addDepartment[0]);
                break;
        }

        //请求的参数
        // Object[] args = jp.getArgs();
        // logEntity.setOperator((String) args[0]);
        //将参数所在的数组转换成json
        // String params = null;
        // try {
        //     params = JacksonUtil.obj2json(args);
        // } catch (Exception e) {
        //     e.printStackTrace();
        // }


        // 操作时间
        logEntity.setTime(new Date());

        // 操作ip
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        logEntity.setIp(IpAdrress.getIpAdrress(request));


        logDao.save(logEntity);



        //获取操作
        // Operation operation = method.getAnnotation(Operation.class);
        // if (operation != null) {
        //     String value = operation.value();
        //     System.out.println("获取操作: " + value);//保存获取的操作
        // }
    }

}
